package JDBCUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class JDBCUtil {
    private static  String DBurl = "jdbc:mysql://localhost:3306/train-buy-ticket-1.0?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; 
    private static  String DBuser = "root"; 
    private static  String DBpass = "980929"; 
    static {
    	//1.加载驱动
    		try {
				DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    }
    
    //获得conn连接
    public static Connection getConn() {
    	try {
    		//2.连接数据库
    		return DriverManager.getConnection(JDBCUtil.DBurl,JDBCUtil.DBuser,JDBCUtil.DBpass);
    	}catch (Exception e) {
			// TODO: handle exception
    		e.printStackTrace();
		}
		return null;
	}
    //释放资源
    public static void Close(Connection conn,Statement st,ResultSet rs) {
    	if(rs!=null) {
	    	try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	
    	if(st!=null) {
	    	try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	
    	if(conn!=null) {
	    	try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}	
    }
    
}
